package br.ufal.tci.nexos.arcolive.beans;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.io.IOException;
import java.io.Serializable;

import javax.media.CannotRealizeException;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.MediaLocator;
import javax.media.NoDataSourceException;
import javax.media.NoPlayerException;
import javax.media.RealizeCompleteEvent;
import javax.swing.JPanel;

/**
 * LVSPlayerInternalFrame.java
 * 
 * <p>
 * This class is a coupling to LVSPlayer class.
 * </p>
 * 
 * 
 * @author <a href="mailto:felipe@labpesquisas.tci.ufal.br">Felipe Barros Pontes</a>.
 * @author <a href="mailto:leandro@labpesquisas.tci.ufal.br">Leandro Melo de
 *         Sales</a>.
 * @since 0.1
 * @version 0.1
 * 
 * <p>
 * <b>Revisions:</b>
 * 
 * <p>
 * <b>20050317 leandro e felipe</b>
 * <ul>
 * <li> First version
 * </ul>
 */

public class PlayerPanel extends JPanel implements ControllerListener,
		Serializable {

	private ArCoLIVEPlayer arcolivePlayer;

	private MediaLocator mediaLocator;

	private Component videoComponent, controlComponent;

	private boolean visibleVisualComponent, visibleControlComponent;

	/**
	 * 
	 */
	public PlayerPanel() {
		super();
		initGUI();
		this.arcolivePlayer = new ArCoLIVEPlayer();
	}

	/**
	 * @param mediaLocator
	 */
	public void setMediaLocator(MediaLocator mediaLocator) {
		this.mediaLocator = mediaLocator;
		this.arcolivePlayer.setMediaLocator(mediaLocator);
	}

	/**
	 * @return
	 */
	public MediaLocator getMediaLocator() {
		return this.mediaLocator;
	}

	/**
	 * @param playerType
	 */
	public void setPlayerType(String playerType) {
		this.arcolivePlayer.setPlayerType(playerType);
	}

	/**
	 * @throws NoDataSourceException
	 * @throws NoPlayerException
	 * @throws IOException
	 * @throws CannotRealizeException
	 */
	public void play() throws CannotPlayStreamException {
		this.arcolivePlayer.play();
		this.arcolivePlayer.addControllerListener(this);
	}

	/**
	 * @return
	 */
	public ArCoLIVEPlayer getArcolivePlayer() {
		return this.arcolivePlayer;
	}

	/**
	 * @param arcolivePlayer
	 */
	public void setArcolivePlayer(ArCoLIVEPlayer arcolivePlayer) {
		this.arcolivePlayer = arcolivePlayer;
	}

	/**
	 * @param visibleControlComponent
	 */
	public void setVisibleControlComponent(boolean visibleControlComponent) {
		this.visibleControlComponent = visibleControlComponent;
	}

	/**
	 * @param visibleVisualComponent
	 */
	public void setVisibleVisualComponent(boolean visibleVisualComponent) {
		this.visibleVisualComponent = visibleVisualComponent;
	}

	/**
	 * 
	 */
	private void initGUI() {
		setLayout(new BorderLayout());
		setVisible(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.media.ControllerListener#controllerUpdate(javax.media.ControllerEvent)
	 */
	public void controllerUpdate(ControllerEvent event) {
		if (event instanceof RealizeCompleteEvent) {
			this.videoComponent = this.arcolivePlayer.getControlComponent();
			if ((this.videoComponent != null) && (this.visibleControlComponent)) {
				add(this.videoComponent, BorderLayout.SOUTH);
			}
			this.controlComponent = this.arcolivePlayer.getVisualComponent();
			if ((this.controlComponent != null)
					&& (this.visibleVisualComponent)) {
				add(this.controlComponent, BorderLayout.CENTER);
			}
			validate();
		}

		if (event instanceof EndOfMediaEvent) {
			if (this.arcolivePlayer.isRepetitive()) {
				try {
					this.arcolivePlayer.play();
				} catch (CannotPlayStreamException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#getPreferredSize()
	 */
	public Dimension getPreferredSize() {
		int w = 0, h = 0;
		if (this.videoComponent != null) {
			Dimension size = this.videoComponent.getPreferredSize();
			w = size.width;
			h = size.height;
		}
		if (this.controlComponent != null) {
			Dimension size = this.controlComponent.getPreferredSize();
			if (w == 0)
				w = size.width;
			h += size.height;
		}
		if (w < 160)
			w = 160;
		return new Dimension(w, h);
	}

}